<template>
  <ekDialog
    v-model="ekVisiable"
    :title="title"
    @submit="handleSubmit"
    :hideBtn="disabled"
  >
    <div class="ek-form" v-loading="loading">
      <el-form
        :model="form"
        :rules="rules"
        label-width="160px"
        ref="forms"
        :disabled="disabled"
        v-if="!loading"
      >
        <el-form-item prop="quotation_name" label="方案名称">
          <ekInput v-model="form.quotation_name" />
        </el-form-item>
        <el-form-item prop="quotation_throwing_weight" label="抛重系数">
          <ekInput v-model="form.quotation_throwing_weight" />
        </el-form-item>
        <el-form-item prop="quotation_min" label="最低消费">
          <ekInput v-model="form.quotation_min" />
        </el-form-item>
        <el-form-item prop="quotation_proportion" label="比重比率">
          <ekInput v-model="form.quotation_proportion" />
        </el-form-item>
         <el-form-item prop="quotation_weight_price" label="首重价格">
          <ekInput v-model="form.quotation_weight_price" />
        </el-form-item>
        
        <el-form-item prop="quotation_plan" label="计费方案">
      
           <ekRadio v-model="form.quotation_plan" :options="quotationOptions" />
        </el-form-item>

        
        <el-form-item prop="quotation_plan_ratio" label="计费系数" v-if="form.quotation_plan == 2">
          <ekInput v-model="form.quotation_plan_ratio" />
        </el-form-item>
        <el-form-item prop="quotation_transport" label="运输方式">
          <ekCheckbox
            v-model="form.quotation_transport"
            :options="modeOptions"
          />
        </el-form-item>
        <el-form-item prop="quotation_type" label="货品类型">
          <ekCheckbox
            v-model="form.quotation_type"
            :options="typeOptions"
          />
        </el-form-item>
        <el-form-item prop="quotation_price" label="计费组合">
          <el-table :data="form.quotation_price" border class="table-box">
              <el-table-column prop="transport_mode" label="运输方式">
                <template v-slot="scope">
                  {{ getModeName(scope.row.transport_mode) }}
                </template>
              </el-table-column>
              <el-table-column prop="transport_type" label="运输类型">
                <template v-slot="scope">
                  {{ getTypeName(scope.row.transport_type) }}
                </template>
              </el-table-column>
              <!-- <el-table-column prop="transport_first_price" label="首重价格">
                <template v-slot="scope">
                  <el-input
                    v-model.number="scope.row.transport_first_price"
                    placeholder="输入首重价格"
                  ></el-input>
                </template>
              </el-table-column> -->
              <el-table-column prop="transport_price" label="续重价格">
                <template v-slot="scope">
                  <el-input
                    v-model.number="scope.row.transport_price"
                    placeholder="输入价格"
                  ></el-input>
                </template>
              </el-table-column>
              <el-table-column prop="transport_weight_cost" label="重量成本">
                <template v-slot="scope">
                  <el-input
                    v-model.number="scope.row.transport_weight_cost"
                    placeholder="输入重量成本"
                  ></el-input>
                </template>
              </el-table-column>
              <el-table-column prop="transport_cube_price" label="立方价格">
                <template v-slot="scope">
                  <el-input
                    v-model.number="scope.row.transport_cube_price"
                    placeholder="输入价格"
                  ></el-input>
                </template>
              </el-table-column>
              <el-table-column prop="transport_cube_cost" label="立方成本">
                <template v-slot="scope">
                  <el-input
                    v-model.number="scope.row.transport_cube_cost"
                    placeholder="输入立方成本"
                  ></el-input>
                </template>
              </el-table-column>
              <el-table-column prop="transport_piece_price" label="按件价格">
                <template v-slot="scope">
                  <el-input
                    v-model.number="scope.row.transport_piece_price"
                    placeholder="输入价格"
                  ></el-input>
                </template>
              </el-table-column>
              <el-table-column prop="transport_piece_cost" label="按件成本">
                <template v-slot="scope">
                  <el-input
                    v-model.number="scope.row.transport_piece_cost"
                    placeholder="输入按件成本"
                  ></el-input>
                </template>
              </el-table-column>
            
            </el-table>
          </el-form-item>
      </el-form>
    </div>
  </ekDialog>
</template>

<script setup>
import {
  watch,
  getCurrentInstance,
  computed,
  reactive,
  ref,
  defineEmits,
  onMounted,
} from "vue";
import { apis } from "../data.js";
import ekDialog from "@/components/business/ekDialog/index.vue";
import ekInput from "@/components/form/ekInput/index.vue";
import ekSelect from "@/components/form/ekSelect/index.vue";
import ekTableSelect from "@/components/form/ekTableSelect/index.vue";
import ekSelectModel from "@/components/form/ekSelectModel/index.vue";
import ekCheckbox from "@/components/form/ekCheckbox/index.vue";
import ekRadio from "@/components/form/ekRadio/index.vue";
import { objectAssignment } from "@/utils/common.js";
import { getDimensionByCode } from "@/utils/sys.js";
const props = defineProps({
  // 显示隐藏
  visiable: {
    type: Boolean,
    default: false,
  },
  rowId: {
    type: [String, Number],
    default: "",
  },
  title: {
    type: String,
    default: "",
  },
  disabled: {
    type: Boolean,
    default: false,
  },
});
const {
  appContext: {
    config: { globalProperties },
  },
} = getCurrentInstance();
const { $storage, $http, $ekLoading, $ekMessage } = globalProperties;

const emit = defineEmits(["update:visiable", "submit"]);

// 是否显示
const ekVisiable = computed({
  get() {
    return props.visiable;
  },
  set(value) {
    emit("update:visiable", value);
  },
});

function handleSubmit(done) {
  let url = props.rowId ? apis.edit : apis.add;
  forms.value.validate((valid) => {
    if (valid) {
      $http
        .request({
          url: url,
          method: props.rowId ? "PUT" : "POST",
          data: {
            ...form.value,
          },
        })
        .then((res) => {
          $ekMessage.success(res.message);
          emit("submit", done);
        })
        .catch((err) => {});
    }
  });
}
const loading = ref(false);
let forms = ref(null);
let form = ref(null);

const initForm = () => {
  form.value = {
    quotation_name: "",
    quotation_throwing_weight: "",
    quotation_proportion: "",
    quotation_min: "",
    quotation_transport: [],
    quotation_type: [],
    quotation_price: [],
    quotation_plan:  "",
    quotation_plan_ratio:  "",
    quotation_weight_price: "",
    id: props.rowId,
  };
};
let rules = reactive({
  quotation_name: [{ required: true, message: "请输入名称", trigger: "blur" }],
  quotation_plan: [{ required: true, message: "请输入计费方案", trigger: "blur" }],
  quotation_transport: [
    { required: true, message: "请选择运输方式", trigger: "blur" },
  ],
  quotation_type: [
    { required: true, message: "请选择货品类型", trigger: "blur" },
  ],
});
const detail = ref({
  
});
const modeOptions = ref([]);
const typeOptions = ref([]);
const getModeName = (mode) => {
  return modeOptions.value.find((item)=>{
    return item.value == mode
  }).label
}
const getTypeName = (type) => {
   return typeOptions.value.find((item)=>{
    return item.value == type
  }).label
}
// 初始化表单
async function init() {
  loading.value = true;
  // 详情或者编辑
  $http
    .get(apis.detail, {
      params: {
        id: props.rowId || null,
      },
    })
    .then((res) => {
      if (Object.keys(res.data).length) {
        form.value = {
          ...res.data.quotation,
        };
        modeOptions.value = res.data.mode;
        typeOptions.value = res.data.type;
        objectAssignment(form.value, res.data);
      }
      loading.value = false;
    });
}
const quotationOptions = ref([]);
watch(
  () => props.visiable,
  (val) => {
    if (val) {
      initForm();
      init();
    }
  }
);
onMounted(async () => {
  quotationOptions.value = await getDimensionByCode("DIM_QUOTATION_STATE");
});
</script>

<style scoped lang="scss"></style>
